﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Emoney.UserTraining.BLL;
using Emoney.UserTraining.Comm;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.Admin.aspx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ajaxMeeting : IHttpHandler
    {
        Traning_NetworkMeeting netmeet = new Traning_NetworkMeeting();

        Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string updateType = Request["updateType"];

                string starll = Request["starll"].ToString();
                string click_num = Request["click_num"].ToString();
                if (Request["id"].ToString() != "")
                {
                    netmeet.Id = Convert.ToInt32(Request["id"].ToString());
                }
                netmeet.click_num = click_num;
                netmeet.satisfaction = Request["txt_satisfaction"].ToString();

                netmeet.mtg_id = Request["mtg_ID"].ToString();

                netmeet.voip_meeting = Request["voip_meeting"].ToString();
                netmeet.mtg_name = System.Web.HttpUtility.UrlDecode(Request["mtg_name"].ToString());
                netmeet.mtg_name = netmeet.mtg_name.Replace("＋", "+");
                netmeet.mtg_conent = HttpContext.Current.Server.UrlDecode(Request["mtg_conent"].ToString());
                netmeet.mtg_conent = netmeet.mtg_conent.Replace("＋", "+");
                netmeet.meeting_type = HttpContext.Current.Server.UrlDecode(Request["meeting_type"].ToString());
                netmeet.meeting_type = netmeet.meeting_type.Replace("＋", "+");
                netmeet.user_type = HttpContext.Current.Server.UrlDecode(Request["user_type"].ToString());
                netmeet.user_type = netmeet.user_type.Replace("＋", "+");
                netmeet.txtteachar = HttpContext.Current.Server.UrlDecode(Request["txtteachar"].ToString());
                netmeet.txtteachar = netmeet.txtteachar.Replace("＋", "+");
                netmeet.txtreateachar = HttpContext.Current.Server.UrlDecode(Request["txtreateachar"].ToString());
                netmeet.txtreateachar = netmeet.txtreateachar.Replace("＋", "+");
                netmeet.lecturerImage = HttpContext.Current.Server.UrlDecode(Request["lecturerImage"].ToString());
                netmeet.host_key = Request["host_key"].ToString();
                netmeet.mtg_password = Request["mtg_password"].ToString();
                netmeet.txt_phone = Request["txt_phone"].ToString();
                netmeet.mtg_view = Request["mtg_view"].ToString();
                netmeet.isRecommend = Request["isRecommend"].ToString();
                netmeet.video_Recommend = "0";
                netmeet.email_flag = Request["email_flag"].ToString();
                netmeet.use_num = Request["use_num"].ToString();

                netmeet.host_id = Request["host_id"].ToString();
                netmeet.pres_id = Request["pres_id"].ToString();
                netmeet.allperson = Request["allperson"].ToString();
                netmeet.pri_name = Request["pri_name"].ToString();
                netmeet.radDisplayType = Request["radDisplayType"].ToString();

                netmeet.ddlarea = HttpContext.Current.Server.UrlDecode(Request["ddlarea"].ToString());
                netmeet.ddlarea = netmeet.ddlarea.Replace("＋", "+");
                netmeet.chblist_pro = HttpContext.Current.Server.UrlDecode(Request["chblist_pro"].ToString());
                netmeet.chblist_pro = netmeet.chblist_pro.Replace("＋", "+");

                netmeet.time_flag = Request["time_flag"].ToString();


                netmeet.Duration_date = Request["Duration_date"].ToString();
                netmeet.before_time = Request["before_time"].ToString();
                netmeet.max_audio_chan = Request["max_audio_chan"].ToString();
                netmeet.max_video_chan = Request["max_video_chan"].ToString();
                netmeet.max_video_size = Request["max_video_size"].ToString();

                netmeet.def_video_size = Request["def_video_size"].ToString();
                netmeet.video_frame_rate = Request["video_frame_rate"].ToString();
                netmeet.max_bandwidth = Request["max_bandwidth"].ToString();
                netmeet.maliu = Request["maliu"].ToString();
                netmeet.mtg_mode = Request["mtg_mode"].ToString();

                netmeet.video_public = Request["video_public"].ToString();
                netmeet.layout_mode = Request["layout_mode"].ToString();
                netmeet.video_layer = Request["video_layer"].ToString();
                netmeet.funSeting = Request["funSeting"].ToString();

                netmeet.createtime = System.DateTime.Now;
                netmeet.updatetime = System.DateTime.Now;

                DateTime class_date = Convert.ToDateTime(Request["class_date"].ToString());

                int before_time = Convert.ToInt32(Request["before_time"].ToString());

                if (click_num == "1")
                {
                    //1400@0300@2@13456@2012-12-29@2013-01-02
                    var str = starll.Split('@');
                    DateTime dstart = new DateTime();
                    try
                    {
                        dstart = Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00");
                    }
                    catch (Exception)
                    {
                        Response.Write("97");
                        //Response.End();
                        return;
                    }

                    if (string.IsNullOrEmpty(updateType))//正在进行或未开始会议判断会议时间（其他修改操作不做判断）
                    {
                        if ((dstart.AddMinutes(before_time * (-1)) - System.DateTime.Now).TotalSeconds <= 0)
                        {
                            Response.Write("99");
                            //Response.End();
                            return;
                        }
                        if (Convert.ToDateTime(str[4]) > Convert.ToDateTime(str[5]))
                        {
                            Response.Write("98");
                            //Response.End();
                            return;
                        }
                    }
                    if (netmeet.Id > 0)
                    {
                        var model = bll.GetModel(netmeet.Id);
                        bll.deletetochecknum(model.click_num, updateType);
                        netmeet.Id = 0;
                    }

                    string guid = new Random().Next(100000, 1000000).ToString();

                    int days = (Convert.ToDateTime(str[5]) - Convert.ToDateTime(str[4])).Days;

                    for (int j = 0; j <= days; j++)
                    {
                        netmeet.Duration_date = (Convert.ToInt32(str[1].Substring(0, 2)) * 60 + Convert.ToInt32(str[1].Substring(2))).ToString();

                        if (Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00").AddDays(j).ToString("yyyy-MM-dd") != System.DateTime.Now.ToString("yyyy-MM-dd"))
                        {
                            netmeet.mtg_id = "";
                        }
                        if (str[2] == "1")
                        {
                            netmeet.click_num = "4^" + guid;
                            netmeet.class_date = Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00").AddDays(j);
                        }
                        else
                        {
                            netmeet.click_num = "3^" + guid;
                            string aa = getTodateWeek(Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00").AddDays(j));
                            if (str[3].Contains(aa))
                            {
                                netmeet.class_date = Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00").AddDays(j);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        netmeet.period = str[3];
                        netmeet.startDate = Convert.ToDateTime(str[4] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00");
                        netmeet.endDate = Convert.ToDateTime(str[5] + " " + str[0].Substring(0, 2) + ":" + str[0].Substring(2) + ":00").AddMinutes(Convert.ToInt32(netmeet.Duration_date));

                        netmeet.mtg_id = Request["mtg_ID1"];
                        netmeet.mtg_password = Request["mtg_password1"];
                        netmeet.Gensee_URL = Request["Gensee_URL"];

                        getList(netmeet);
                        netmeet.Id = 0;
                    }
                }
                else
                {
                    if (updateType == "")//正在进行或未开始会议判断会议时间（其他修改操作不做判断）
                    {
                        if ((class_date.AddMinutes(before_time * (-1)) - System.DateTime.Now).TotalSeconds <= 0)
                        {
                            Response.Write("99");
                            //Response.End();
                            return;
                        }
                    }
                    netmeet.class_date = Convert.ToDateTime(Request["class_date"]);
                    netmeet.startDate = Convert.ToDateTime(netmeet.class_date);
                    netmeet.endDate = Convert.ToDateTime(netmeet.class_date).AddMinutes(Convert.ToInt32(netmeet.Duration_date));
                    netmeet.period = "0";

                    if (netmeet.Id > 0)
                    {
                        netmeet.mtg_id = Request["mtg_ID1"];
                        netmeet.mtg_password = Request["mtg_password1"];
                        netmeet.Gensee_URL = Request["Gensee_URL"];
                    }

                    getList(netmeet);
                }
                bll.ThreadAllMtg();


                Response.Write("1");
                //Response.End();
            }
            catch (Exception exp)
            {
                Response.Write(exp.Message);
                //Response.End();
            }
        }

        public void getList(Traning_NetworkMeeting neet)
        {
            int i = bll.ModifyNetworkMeeting(neet);
            if (i <= 0)
            {
                Response.Write("-99");
                Response.End();
            }
        }


        public string getTodateWeek(DateTime dttime)
        {
            string nums = "";
            if (dttime != null)
            {
                DayOfWeek dy = dttime.DayOfWeek;

                switch (dy)
                {
                    case DayOfWeek.Friday:
                        nums = "5";
                        break;
                    case DayOfWeek.Monday:
                        nums = "1";
                        break;
                    case DayOfWeek.Saturday:
                        nums = "6";
                        break;
                    case DayOfWeek.Sunday:
                        nums = "7";
                        break;
                    case DayOfWeek.Thursday:
                        nums = "4";
                        break;
                    case DayOfWeek.Tuesday:
                        nums = "2";
                        break;
                    case DayOfWeek.Wednesday:
                        nums = "3";
                        break;
                }
            }
            return nums;
        }


        public HttpRequest Request
        {
            get { return HttpContext.Current.Request; }
        }

        public HttpResponse Response
        {
            get { return HttpContext.Current.Response; }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}